/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clientserver_test_tim;

import java.net.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author timbeer
 */
class ServerThread extends Thread{
    
    
    private Server server;
    private Socket client;
    
    

    public ServerThread(Server server, Socket client) {
        
        this.server = server;
        this.client = client;
        
        start();
    }
    
    
    public void run(){
        try {
            ObjectInputStream ois = new ObjectInputStream(client.getInputStream());
            
            while(true){
                
                //read object
                Person p = (Person) ois.readObject();
                
                System.out.println("Object read");
                
                server.sendToAll(p);
                
            }
            
            
            
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            server.removeConnection(client);
        }
    }
    
}
